package cn.edu.hubu.hubuhotel.service.impl;

import cn.edu.hubu.hubuhotel.entity.User;
import cn.edu.hubu.hubuhotel.mapper.UserMapper;
import cn.edu.hubu.hubuhotel.service.UserService;
import cn.edu.hubu.hubuhotel.vo.ResultVO;
import cn.hutool.crypto.digest.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ResultVO checkAccount(String account) {
        User user = userMapper.selectByAccount(account);
        if(user != null){
            return new ResultVO(500,"账号不可用",account);
        }else{
            return new ResultVO(200,"账号可用",account);
        }
    }

    @Override
    public ResultVO saveUser(User user) {
        String loginPwd = user.getLoginPwd();//明文
        String newPwd = DigestUtil.md5Hex(loginPwd);//密文 32位
        user.setLoginPwd(newPwd);

        int i = userMapper.insertUser(user);

        if(i > 0){
            return new ResultVO(200,"注册成功！",null);
        }else{
            return new ResultVO(500,"注册失败！",null);
        }
    }
    @Override
    public ResultVO checkLogin(String loginAccount,String loginPwd) {
        User user=userMapper.selectByAccountAndPwd(loginAccount,loginPwd);
        if(user != null){
            return new ResultVO(200,"登录成功！",null);
        }else {
            return new ResultVO(500,"登录失败！",null);
        }
    }


    @Override
    public ResultVO checkLogin3(String loginAccount,String loginPwd,int typeId) {
        User user=userMapper.selectByAccount(loginAccount);
        if(user == null){
            return new ResultVO(500,"账号错误，登录失败！",null);
        }
        String newPwd = DigestUtil.md5Hex(loginPwd);
        String dbPwd = user.getLoginPwd();
        int dbTypeId=user.getUserTypeId();
        if(newPwd.equals(dbPwd)){
            if(dbTypeId == typeId){
                user.setLoginPwd("******");
                return new ResultVO(200,"登录成功！",user);
            }
            else{
                return new ResultVO(404,"类型错误，登录失败！",null);
            }
        }else {
            return new ResultVO(500,"密码错误，登录失败！",null);
        }
    }

    @Override
    public ResultVO<User> searchUser(String phone){
        User user = userMapper.selectUser(phone);
        if (user != null) {
            return new ResultVO<>(200, "查询成功", user);
        } else {
            return new ResultVO<>(404, "用户不存在", null);
        }
    }

    @Override
    public ResultVO<List<User>> divideUser(int type) {
        List<User> users = userMapper.selectUserByType(type);
        if (users != null && !users.isEmpty()) {
            return new ResultVO<>(200, "切换类型成功", users);
        } else {
            return new ResultVO<>(404, "没有找到该类型的用户", null);
        }
    }

    @Override
    public ResultVO<String> increaseUser(User user) {
        try {
            int i = userMapper.addUser(user);
            if(i > 0){
                return new ResultVO(200, "插入新数据成功", null);
            } else {
                return new ResultVO(500, "插入新数据失败，请重试", null);
            }
        } catch (DuplicateKeyException e) {
            // 捕获主键冲突异常
            return new ResultVO(400, "插入数据失败：RoomId已经存在", null);
        } catch (Exception e) {
            // 捕获其他可能的异常
            return new ResultVO(500, "插入数据失败，请重试", null);
        }
    }

    @Override
    public ResultVO<String> reviseUser(User user){
        int result = userMapper.changeUser(user);
        if (result > 0) {
            return new ResultVO<>(200, "用户更新成功", "操作成功");
        } else {
            return new ResultVO<>(500, "用户更新失败", null);
        }
    }

    @Override
    public ResultVO<String> cancleUser(int id){
        int result = userMapper.deleteUser(id);
        if (result > 0) {
            return new ResultVO<>(200, "用户删除成功", "操作成功");
        } else {
            return new ResultVO<>(500, "用户删除失败", null);
        }
    }
    @Override
    public ResultVO updateUser(User user) {
        int i=userMapper.updateUserByAccount(user);
        if(i > 0){
            return new ResultVO(200,"个人信息更新成功！",user);
        }else {
            return new ResultVO(500,"个人信息更新失败！",null);
        }
    }
}
